"""
结算账单查询相关模型

@author liaochangyi
@since 2025-01-25
"""

from typing import Dict, Any, List
from jlpay.model.base import OrgBaseReq, BaseResponse


class BillDetail:
    """账单明细信息"""
    
    def __init__(self, detail_dict=None):
        """
        初始化账单明细
        
        Args:
            detail_dict: 明细字典
        """
        self.start_busi_date = None  # 开始营业日期，yyyy-MM-dd
        self.end_busi_date = None  # 结束营业日期，yyyy-MM-dd
        self.bill_date = None  # 结算时间，yyyy-MM-dd
        self.remark = None  # 备注，付款失败原因
        self.pay_status = None  # 付款状态：0-待付款 1-付款成功 2-付款失败 3-无需付款
        self.pay_amt = None  # 付款金额
        self.trans_count = None  # 交易笔数
        self.settle_account = None  # 结算账户
        self.refund_amount = None  # 退票金额，单位：分
        self.return_date = None  # 退票日期
        self.return_reason = None  # 退票原因
        self.account_no = None  # 退票卡号
        
        if detail_dict:
            self.start_busi_date = detail_dict.get('start_busi_date')
            self.end_busi_date = detail_dict.get('end_busi_date')
            self.bill_date = detail_dict.get('bill_date')
            self.remark = detail_dict.get('remark')
            self.pay_status = detail_dict.get('pay_status')
            self.pay_amt = detail_dict.get('pay_amt')
            self.trans_count = detail_dict.get('trans_count')
            self.settle_account = detail_dict.get('settle_account')
            self.refund_amount = detail_dict.get('refund_amount')
            self.return_date = detail_dict.get('return_date')
            self.return_reason = detail_dict.get('return_reason')
            self.account_no = detail_dict.get('account_no')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'start_busi_date': self.start_busi_date,
            'end_busi_date': self.end_busi_date,
            'bill_date': self.bill_date,
            'remark': self.remark,
            'pay_status': self.pay_status,
            'pay_amt': self.pay_amt,
            'trans_count': self.trans_count,
            'settle_account': self.settle_account,
            'refund_amount': self.refund_amount,
            'return_date': self.return_date,
            'return_reason': self.return_reason,
            'account_no': self.account_no
        }


class SettleBillQueryRequest(OrgBaseReq):
    """结算账单查询请求"""

    API_URL = "/open/bill/settle-bill/query"

    def __init__(self):
        """初始化结算账单查询请求"""
        super().__init__()
        self.merch_no = None  # 商户号
        self.start_busi_date = None  # 开始营业日期，订单交易日期（订单交易日期/订单清算日期，二选一），结束时间减开始时间不能超过30天， yyyy-MM-dd
        self.end_busi_date = None  # 结束营业日期，订单交易日期（订单交易日期/订单清算日期，二选一），结束时间减开始时间不能超过30天， yyyy-MM-dd
        self.start_settle_date = None  # 开始结算日期，订单结算日期（订单交易日期/订单清算日期，二选一）， yyyy-MM-dd
        self.end_settle_date = None  # 结束结算日期，订单结算日期（订单交易日期/订单清算日期，二选一）， yyyy-MM-dd
        self.pay_status = None  # 付款状态，0-待付款 1-付款成功 2-付款失败 3-无需付款

    def set_merch_no(self, merch_no: str):
        """
        设置商户号

        Args:
            merch_no: 商户号

        Returns:
            SettleBillQueryRequest实例
        """
        self.merch_no = merch_no
        return self

    def set_start_busi_date(self, start_busi_date: str):
        """
        设置开始营业日期

        Args:
            start_busi_date: 开始营业日期

        Returns:
            SettleBillQueryRequest实例
        """
        self.start_busi_date = start_busi_date
        return self

    def set_end_busi_date(self, end_busi_date: str):
        """
        设置结束营业日期

        Args:
            end_busi_date: 结束营业日期

        Returns:
            SettleBillQueryRequest实例
        """
        self.end_busi_date = end_busi_date
        return self

    def set_start_settle_date(self, start_settle_date: str):
        """
        设置开始结算日期

        Args:
            start_settle_date: 开始结算日期

        Returns:
            SettleBillQueryRequest实例
        """
        self.start_settle_date = start_settle_date
        return self

    def set_end_settle_date(self, end_settle_date: str):
        """
        设置结束结算日期

        Args:
            end_settle_date: 结束结算日期

        Returns:
            SettleBillQueryRequest实例
        """
        self.end_settle_date = end_settle_date
        return self

    def set_pay_status(self, pay_status: str):
        """
        设置付款状态

        Args:
            pay_status: 付款状态

        Returns:
            SettleBillQueryRequest实例
        """
        self.pay_status = pay_status
        return self

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'merch_no': self.merch_no,
            'start_busi_date': self.start_busi_date,
            'end_busi_date': self.end_busi_date,
            'start_settle_date': self.start_settle_date,
            'end_settle_date': self.end_settle_date,
            'pay_status': self.pay_status
        })
        return data


class SettleBillQueryResponse(BaseResponse):
    """结算账单查询响应"""

    def __init__(self, response_dict=None):
        """
        初始化结算账单查询响应

        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.err_msg = None  # 错误信息
        self.merch_no = None  # 商户号
        self.start_busi_date = None  # 开始营业日期，yyyy-MM-dd
        self.end_busi_date = None  # 结束营业日期
        self.start_settle_date = None  # 开始结算日期，yyyy-MM-dd
        self.end_settle_date = None  # 结束结算日期
        self.pay_amt = None  # 付款金额
        self.trans_count = None  # 交易笔数
        self.bill_detail_list = None  # 账单明细列表

        if response_dict:
            self.err_msg = response_dict.get('err_msg')
            self.merch_no = response_dict.get('merch_no')
            self.start_busi_date = response_dict.get('start_busi_date')
            self.end_busi_date = response_dict.get('end_busi_date')
            self.start_settle_date = response_dict.get('start_settle_date')
            self.end_settle_date = response_dict.get('end_settle_date')
            self.pay_amt = response_dict.get('pay_amt')
            self.trans_count = response_dict.get('trans_count')
            bill_detail_list_data = response_dict.get('bill_detail_list', [])
            if bill_detail_list_data:
                self.bill_detail_list = [BillDetail(detail) for detail in bill_detail_list_data]

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'err_msg': self.err_msg,
            'merch_no': self.merch_no,
            'start_busi_date': self.start_busi_date,
            'end_busi_date': self.end_busi_date,
            'start_settle_date': self.start_settle_date,
            'end_settle_date': self.end_settle_date,
            'pay_amt': self.pay_amt,
            'trans_count': self.trans_count,
            'bill_detail_list': [detail.to_dict() for detail in self.bill_detail_list] if self.bill_detail_list else []
        })
        return data 